﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Meeting.Public;
using Newtonsoft.Json;

namespace Meeting.Web.Admin.BaseSet
{
    /// <summary>
    /// SubOfficeOperation 的摘要说明
    /// </summary>
    public class SubOfficeOperation : IHttpHandler
    {

        string methodName = "";
        int nPageIndex = 1;
        int nPageSize = 20;
        BLL.SubOffice bll = new BLL.SubOffice();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json;charset=utf-8";
            //context.Response.ContentType = "text/plain";
            methodName = context.Request.Params["method"];
            nPageIndex = CommonFunction.varToIntDef(context.Request.Params["page"], 1);
            nPageSize = CommonFunction.varToIntDef(context.Request.Params["rows"], 15);
            switch (methodName)
            {
                case "GetSubOffice":   //获取json数据
                    context.Response.Write(bll.GetPage(nPageIndex, nPageSize, " 1=1 "));
                    break;
                case "Save":
                    context.Response.Write(SaveData(context));
                    break;
                case "Delete":
                    context.Response.Write(DeleteData(context));
                    break;
            }
        }

        /// <summary>
        /// 保存会议室
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string SaveData(HttpContext context)
        {
            Meeting.Model.SubOffice model = new Model.SubOffice();
            string id = CommonFunction.VarToStr(context.Request.Params["id"]);
            //if (id == "")
            //    model.Id = Guid.NewGuid();
            //else
            //    model.Id = Guid.Parse(id);
            model.Id = Convert.ToInt32(id);
            model.Number = CommonFunction.varToIntDef(context.Request.Params["number"],1);
            model.Name = CommonFunction.removeAttackOfSql(context.Request.Params["name"]);
            //
            Model.JsonResponse res = new Model.JsonResponse();
            if ((id == "0" ? bll.Add(model) : bll.Update(model)))
            {
                res.success = true;
            }
            else
            {
                res.success = false;
                res.Message = bll.errorString;
            }
            return JsonConvert.SerializeObject(res);
        }

        //删除分局
        private string DeleteData(HttpContext context)
        {
            string id = context.Request["id"];
            int ids = 0;

            try
            {
                ids = Convert.ToInt32(id);
            }catch{
                ids =0;
            }

            Model.JsonResponse rsp = new Model.JsonResponse();

            try
            {
                //提交数据            
                if (bll.Delete(ids)) 
                {
                    rsp.success = true;
                }
                else
                {
                    rsp.success = false;
                    rsp.Message = bll.errorString;
                }
            }
            catch (Exception ex)
            {
                rsp.success = false;
                rsp.Message = ex.Message;
            }
            return JsonConvert.SerializeObject(rsp);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}